package hoang.hut.app.tool;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class AnimationHandle {
	/**
	 * dat anh vao vi tri tren man hinh
	 * 
	 * @param view
	 * @param left
	 * @param top
	 * @param right
	 * @param botton
	 */
	public static void setPositionView(View view,int left, int top, int right, int botton){
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
				view.getLayoutParams().width, 
				view.getLayoutParams().height);
		layoutParams.setMargins(left, top, right, botton);
		view.setLayoutParams(layoutParams);
	}
	/**
	 * animation frame by frame
	 * 
	 * @param imageView: ImageView hien thi anh
	 * @param drawable: anh vecto, dat trong thu muc drawable (.xml)
	 */
	public static void animationFrameByFrame(ImageView imageView, int drawable) {
		imageView.setBackgroundResource(drawable);
		AnimationDrawable animationDrawable = (AnimationDrawable) imageView
				.getBackground();
		if (animationDrawable.isRunning()) {
			animationDrawable.stop();
		} else {
			animationDrawable.start();
		}
	}
	/**
	 * animation Tween
	 * 
	 * @param context
	 * @param animation
	 * @param imageView
	 * @param idAnimation: id cua file xml, dinh nghia trong thu muc anim
	 * @param isListener: xet lang nghe (true: co, false: ko)
	 */
	public static void animationTween(Context context, ImageView imageView, int idAnimation, boolean isListener){
		Animation animation = AnimationUtils.loadAnimation(context, idAnimation);
		if(isListener){
			animation.setAnimationListener((AnimationListener) context);
		}
		imageView.startAnimation(animation);
	}
	
	public void expandView(final View v, final int h1, final int h2, int duration){
		v.measure(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		
		Animation animation = new Animation() {
			@Override
			protected void applyTransformation(float interpolatedTime,
					Transformation t) {
				// TODO Auto-generated method stub
				super.applyTransformation(interpolatedTime, t);
				
				int a = interpolatedTime == 1 ? h2
						: (int) ((h2-h1)*interpolatedTime+h1);
					v.getLayoutParams().height = a;
					v.requestLayout();
			}
		};
		
		animation.setDuration(duration);
		v.startAnimation(animation);
		
	}
}
